//
//  NRWBuyCarPriceView.m
//  NRWMall
//
//  Created by 邢毅 on 2018/6/8.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWBuyCarPriceView.h"
#import "NRWPriceChooseCell.h"
#import "NRWBuyCarConditionModel.h"
#import "ZPSlider.h"
#import "NRWBuyCarFilterManager.h"
#import "NRWBuyCarPriceHeaderView.h"

@interface NRWBuyCarPriceView()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) NSArray *priceSliderTitleArray;

@end

@implementation NRWBuyCarPriceView
{
    UICollectionView *_mCollectionView; /**< collectionView */
    NSString *_mPriceSliderDetailString; /**< 价格滑块header string */
    
    NRWBuyCarPriceModel *_mTempSelectPriceModel;
    NSArray<NRWBuyCarPriceModel *> *_mTempPriceItemModelArray;
}

- (void)setSelectPriceModel:(NRWBuyCarPriceModel *)selectPriceModel priceItemModelArray:(NSArray<NRWBuyCarPriceModel *> *)priceItemModelArray {
    
    _mTempSelectPriceModel = selectPriceModel.copy;
    _mTempPriceItemModelArray = priceItemModelArray;
    _mPriceSliderDetailString = selectPriceModel.priceTitle;
    
    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    [_mCollectionView reloadData];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        [self setupUI];
        
        [self prepare];
    }
    return self;
}

- (void)setupUI {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 19;
    layout.minimumInteritemSpacing = 15;
    
    _mCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _mCollectionView.delegate = self;
    _mCollectionView.dataSource = self;
    _mCollectionView.backgroundColor = [UIColor whiteColor];
    [_mCollectionView registerClass:NRWBuyCarPriceHeaderView.class forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [_mCollectionView registerClass:NRWPriceChooseCell.class forCellWithReuseIdentifier:@"cell1"];
    [_mCollectionView registerClass:UICollectionViewCell.class forCellWithReuseIdentifier:@"cell2"];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (section == 0) {
        return _mTempPriceItemModelArray.count;
    } else {
        return 1;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        NRWPriceChooseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
        NRWBuyCarPriceModel *model = _mTempPriceItemModelArray[indexPath.item];
        cell.priceTitle = model.priceTitle;
        cell.isSelect = _mTempSelectPriceModel.currentSelectIndex == indexPath.item;
        
        return cell;
    } else {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell2" forIndexPath:indexPath];
        
        ZPSlider *slider;
        
        if ([cell.contentView viewWithTag:999]) {// 有slider
            
            slider = [cell.contentView viewWithTag:999];
        } else {// 没有
            
            slider = [[ZPSlider alloc] initWithFrame:CGRectMake(47, 28, SCREEN_WIDTH() - 47 * 2, 55) sliderTitles:self.priceSliderTitleArray defaultFirstIndex:0 defaultLastIndex:self.priceSliderTitleArray.count - 1 sliderImage:getImage(@"icon_slider")];
            slider.tag = 999;
            [cell.contentView addSubview:slider];
        }
        
        if (_mTempSelectPriceModel.isCustomePrice) {
            slider.firstSelectedInedx = _mTempSelectPriceModel.selectedFirstIndex;
            slider.lastSelectedInedx = _mTempSelectPriceModel.selectedLastIndex;
        } else {
            slider.firstSelectedInedx = 0;
            slider.lastSelectedInedx = self.priceSliderTitleArray.count - 1;
        }
        
        MJWeakSelf
        slider.block = ^(int firstIndexPath, int lastIndexPath) {
            __strong typeof(weakSelf)strongSelf = weakSelf;
            
            if (firstIndexPath == 0 && lastIndexPath == self.priceSliderTitleArray.count - 1) {
                
                strongSelf->_mPriceSliderDetailString = @"不限";
                strongSelf->_mTempSelectPriceModel.priceFrom = nil;
                strongSelf->_mTempSelectPriceModel.priceTo = nil;
            } else if (firstIndexPath != 0 && lastIndexPath == strongSelf.priceSliderTitleArray.count - 1) {
                strongSelf->_mPriceSliderDetailString = [NSString stringWithFormat:@"%@万以上", strongSelf.priceSliderTitleArray[firstIndexPath]];
                strongSelf->_mTempSelectPriceModel.priceFrom = weakSelf.priceSliderTitleArray[firstIndexPath];
                strongSelf->_mTempSelectPriceModel.priceTo = nil;
            } else {
                strongSelf->_mPriceSliderDetailString = [NSString stringWithFormat:@"%@-%@万", strongSelf.priceSliderTitleArray[firstIndexPath], strongSelf.priceSliderTitleArray[lastIndexPath]];
                strongSelf->_mTempSelectPriceModel.priceFrom = weakSelf.priceSliderTitleArray[firstIndexPath];
                strongSelf->_mTempSelectPriceModel.priceTo = weakSelf.priceSliderTitleArray[lastIndexPath];
            }
            
            strongSelf->_mTempSelectPriceModel.selectedFirstIndex = firstIndexPath;
            strongSelf->_mTempSelectPriceModel.selectedLastIndex = lastIndexPath;
            strongSelf->_mTempSelectPriceModel.isCustomePrice = YES;
            strongSelf->_mTempSelectPriceModel.currentSelectIndex = -1;
            strongSelf->_mTempSelectPriceModel.priceTitle = strongSelf->_mPriceSliderDetailString;
            
            [CATransaction begin];
            [CATransaction setDisableActions:YES];
            [strongSelf->_mCollectionView reloadData];
    
            if ([weakSelf.delegate respondsToSelector:@selector(buyCarPriceView:didSelectPriceWithPriceModel:)]) {
                
                [weakSelf.delegate buyCarPriceView:weakSelf didSelectPriceWithPriceModel:strongSelf->_mTempSelectPriceModel];
            }
        };

        return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 1) {
        return;
    }
    
    NRWBuyCarPriceModel *model = _mTempPriceItemModelArray[indexPath.item];
    _mTempSelectPriceModel.selectedFirstIndex = 0;
    _mTempSelectPriceModel.selectedLastIndex = self.priceSliderTitleArray.count - 1;
    _mTempSelectPriceModel.isCustomePrice = NO;
    _mTempSelectPriceModel.currentSelectIndex = indexPath.item;
    _mTempSelectPriceModel.priceFrom = model.priceFrom;
    _mTempSelectPriceModel.priceTo = model.priceTo;
    _mTempSelectPriceModel.priceTitle = model.priceTitle;
    _mPriceSliderDetailString = model.priceTitle;
    
    if ([self.delegate respondsToSelector:@selector(buyCarPriceView:didSelectPriceWithPriceModel:)]) {

        [self.delegate buyCarPriceView:self didSelectPriceWithPriceModel:_mTempSelectPriceModel];
    }
    
    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    [_mCollectionView reloadData];
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader) {
        
        NRWBuyCarPriceHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        header.detailString = _mPriceSliderDetailString;
        return header;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    if (section == 1) {
        return CGSizeMake(SCREEN_WIDTH(), 15);
    }
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        CGFloat itemW = (SCREEN_WIDTH() - 16 * 2 - 15 * 3) / 4;
        
        return CGSizeMake(itemW, itemW * 0.4);
    } else {
        
        return CGSizeMake(SCREEN_WIDTH(), 130);
    }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    if (section == 0) {
        return UIEdgeInsetsMake(18, 15, 40, 15);
    } else {
        return UIEdgeInsetsZero;
    }
}

#pragma mark - overrideSuperMethod
- (UIView *)bulletContentView {
    
    return _mCollectionView;
}

- (BulletContentViewAlignment)contentViewAlignment {
    
    return BulletAlignmentTop;
}

- (BOOL)touchBackgroundCanClose {
    
    return YES;
}

- (CGFloat)contentViewHeight {
    
    return 314;
}

- (void)showForView:(UIView *)view {
    
    if ([self.delegate respondsToSelector:@selector(buyCarPriceViewWillShow:)]) {
        [self.delegate buyCarPriceViewWillShow:self];
    }
    [super showForView:view];
}

- (void)dismiss {
    
    if ([self.delegate respondsToSelector:@selector(buyCarPriceViewWillDismiss:)]) {
        [self.delegate buyCarPriceViewWillDismiss:self];
    }
    
    [super dismiss];
}

#pragma getter
- (NSArray *)priceSliderTitleArray {
    if (_priceSliderTitleArray == nil) {
        _priceSliderTitleArray = [NRWBuyCarFilterManager getPriceSliderTitleArray];
    }
    return _priceSliderTitleArray;
}

@end
